package com.ruoyi.framework.web.service;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.pay.domain.PayCoinUserForm;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wq.domain.WqTask;
import com.ruoyi.wq.domain.vo.WqTaskVO;
import com.ruoyi.wq.service.IWqTaskService;

import cn.hutool.core.date.DateUtil;

@Component
public class TaskService {
	
	@Autowired
	private IWqTaskService wqTaskService;
	
	@Autowired
	private ISysUserService sysUserService;
	
	@Autowired
	private CoinUserService coinUserService;

	public List<WqTask> getTaskList(WqTaskVO wqTask) {
		List<WqTask> list = new ArrayList<WqTask>();
		QueryWrapper<WqTask> qw = new QueryWrapper<>();
		WqTask entity = new WqTask();
		entity.setStatus("0");
		qw.setEntity(entity);
		qw.orderByDesc("sort");
		List<WqTask> taskList = wqTaskService.list(qw);
		SysUser user = sysUserService.getById(wqTask.getUserId());
		for(WqTask task : taskList){
			if("rate".equals(task.getTaskType())){
				// 判断是否评价
				if("0".equals(user.getIsRate())){
					list.add(task);
				}
			}else if("add".equals(task.getTaskType())){
				// 判断是否添加
				if("0".equals(user.getIsAdd())){
					list.add(task);
				}
			}else if("ad".equals(task.getTaskType())){
				String nowDate = DateUtil.formatDate(DateUtil.date());
				// 判断是否添加
				if(user.getGameNum() < 3){
					list.add(task);
				}else{
					if(StringUtils.isNotBlank(user.getSignDate())){
						if(!nowDate.equals(user.getSignDate())){
							list.add(task);
						}
					}
				}
			}else{
				list.add(task);
			}
		}
		return list;
	}

	public List<WqTask> TaskOk(WqTaskVO wqTask) {
		WqTask task = wqTaskService.getById(wqTask.getId());
		SysUser user = sysUserService.getById(wqTask.getUserId());
		if("rate".equals(task.getTaskType())){
			if("0".equals(user.getIsRate())){
				// 评价奖励
				PayCoinUserForm payCoinUserForm = new PayCoinUserForm();
		    	payCoinUserForm.setCoinNum(task.getTaskCoin());
		    	payCoinUserForm.setCoinType("0");
		    	payCoinUserForm.setRemark("好评奖励");
		    	payCoinUserForm.setUserId(user.getUserId());
		    	coinUserService.savePayCoinUser(payCoinUserForm);
		    	
		    	user.setIsRate("1");
		    	sysUserService.updateById(user);
			}
		}else if("add".equals(task.getTaskType())){
			if("0".equals(user.getIsAdd())){
				// 添加奖励
				PayCoinUserForm payCoinUserForm = new PayCoinUserForm();
		    	payCoinUserForm.setCoinNum(task.getTaskCoin());
		    	payCoinUserForm.setCoinType("0");
		    	payCoinUserForm.setRemark("添加奖励");
		    	payCoinUserForm.setUserId(user.getUserId());
		    	coinUserService.savePayCoinUser(payCoinUserForm);
		    	
		    	user.setIsAdd("1");
		    	sysUserService.updateById(user);
			}
		}else if("sign".equals(task.getTaskType())){
			String nowDate = DateUtil.formatDate(DateUtil.date());
			if(StringUtils.isBlank(user.getSignDate())){
				// 签到奖励
				PayCoinUserForm payCoinUserForm = new PayCoinUserForm();
		    	payCoinUserForm.setCoinNum(task.getTaskCoin());
		    	payCoinUserForm.setCoinType("0");
		    	payCoinUserForm.setRemark("签到奖励");
		    	payCoinUserForm.setUserId(user.getUserId());
		    	coinUserService.savePayCoinUser(payCoinUserForm);
		    	
		    	user.setSignDate(nowDate);
		    	sysUserService.updateById(user);
			}else{
				if(nowDate.equals(user.getSignDate())){
					// 今天已签到
					throw new ServiceException("今天已签到，明天才能签到");
				}else{
					// 签到奖励
					PayCoinUserForm payCoinUserForm = new PayCoinUserForm();
			    	payCoinUserForm.setCoinNum(task.getTaskCoin());
			    	payCoinUserForm.setCoinType("0");
			    	payCoinUserForm.setRemark("签到奖励");
			    	payCoinUserForm.setUserId(user.getUserId());
			    	coinUserService.savePayCoinUser(payCoinUserForm);
			    	
			    	user.setSignDate(nowDate);
			    	sysUserService.updateById(user);
				}
			}
		}else if("ad".equals(task.getTaskType())){
			String nowDate = DateUtil.formatDate(DateUtil.date());
			if(StringUtils.isBlank(user.getSignDate())){
				// 签到奖励
				PayCoinUserForm payCoinUserForm = new PayCoinUserForm();
		    	payCoinUserForm.setCoinNum(20);
		    	payCoinUserForm.setCoinType("0");
		    	payCoinUserForm.setRemark("签到奖励");
		    	payCoinUserForm.setUserId(user.getUserId());
		    	coinUserService.savePayCoinUser(payCoinUserForm);
		    	
		    	user.setSignDate(nowDate);
		    	user.setGameNum(0);
		    	sysUserService.updateById(user);
			}else{
				if(!nowDate.equals(user.getSignDate())){
					// 签到奖励
					PayCoinUserForm payCoinUserForm = new PayCoinUserForm();
			    	payCoinUserForm.setCoinNum(20);
			    	payCoinUserForm.setCoinType("0");
			    	payCoinUserForm.setRemark("签到奖励");
			    	payCoinUserForm.setUserId(user.getUserId());
			    	coinUserService.savePayCoinUser(payCoinUserForm);
			    	
			    	user.setSignDate(nowDate);
			    	user.setGameNum(0);
			    	sysUserService.updateById(user);
				}
			}
			
			if(user.getGameNum() < 3){
				// 广告奖励
				PayCoinUserForm payCoinUserForm = new PayCoinUserForm();
		    	payCoinUserForm.setCoinNum(task.getTaskCoin());
		    	payCoinUserForm.setCoinType("0");
		    	payCoinUserForm.setRemark("观看广告奖励");
		    	payCoinUserForm.setUserId(user.getUserId());
		    	coinUserService.savePayCoinUser(payCoinUserForm);
		    	Integer gameNum = user.getGameNum() + 1;
		    	user.setGameNum(gameNum);
		    	sysUserService.updateById(user);
			}else{
				throw new ServiceException("今天观看广告次数已用完", 520);
			}
		}
		return getTaskList(wqTask);
	}

}
